﻿using System;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
namespace Portal
{
	/// <summary>
	/// Summary description for ViewFolderShare.
	/// </summary>
	public partial class MediaHelper : System.Web.UI.Page
	{
		public string sPicture;
		string strAuthor,sFolder;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if(!IsPostBack)
			{
				//Response.Write("tg="+Request.QueryString["user"]);
				strAuthor=Request.QueryString["User"];
				sFolder=Request.PhysicalApplicationPath+"images\\"+strAuthor;
				if(!System.IO.Directory.Exists(sFolder) )
				{
					System.IO.Directory.CreateDirectory(sFolder); 
				}
				//đưa dữ liệ vào combo Type
				DataTable objType=MediaType();
				cboMediaType.DataSource=objType;
				cboMediaType.DataMember=objType.TableName;
				cboMediaType.DataValueField=objType.Columns[0].ColumnName;
				cboMediaType.DataTextField=objType.Columns[1].ColumnName;
				cboMediaType.DataBind();

				//Chuyển nội dung đc chọn vào list
				cboMediaType.SelectedIndex=0;
				SourcList(lstPicture,cboMediaType.SelectedValue, sFolder);
			}
			//đưa sự kiện vào OK button
			//string filename="images/"+strAuthor+"/"+lstPicture.SelectedItem.Text;
			string _strChildSelect="ChildImage('"+strAuthor+"');return false";
			btnOK.Attributes.Add("Onclick",_strChildSelect);
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion
		protected override void LoadViewState(object savedState)
		{
			base.LoadViewState (savedState);
			strAuthor=ViewState["strAuthor"].ToString();
			sFolder=ViewState["sFolder"].ToString();
		}
		protected override object SaveViewState()
		{
			ViewState["strAuthor"]=strAuthor;
			ViewState["sFolder"]=sFolder;
			return base.SaveViewState ();
		}
 
        private void SourcList(ListBox _objLstSourc,string _strType, string _sFolder)
        {
			//string _sFolder=Server.MapPath("Images/Share");
        	string[] _arrListFiles= System.IO.Directory.GetFiles(_sFolder);
		    Hashtable _objTempSrc= new Hashtable();
			int _intCount=_arrListFiles.Length ;
		    for(int i=0;i<_intCount; i++)
		    {
		    	string _sFile=System.IO.Path.GetFileName(_arrListFiles[i]) ;
				switch(_strType)
				{
					case "1"://là ảnh
						if((_sFile.ToLower().IndexOf(".jpg".ToLower())>0)||(_sFile.ToLower().IndexOf(".gif".ToLower())>0)||(_sFile.ToLower().IndexOf(".png".ToLower())>0))
							_objTempSrc.Add(_sFile,_sFile) ;
						break;
					case "2"://là flash
						if(_sFile.ToLower().IndexOf(".swf".ToLower())>0)
							_objTempSrc.Add(_sFile,_sFile) ;
						break;
					case "3"://là film
						if((_sFile.ToLower().IndexOf(".avi".ToLower())>0)||(_sFile.ToLower().IndexOf(".mpg".ToLower())>0)||(_sFile.ToLower().IndexOf(".dat".ToLower())>0)||(_sFile.ToLower().IndexOf(".wmv".ToLower())>0))
							_objTempSrc.Add(_sFile,_sFile) ;
						break;
				}
		    }
			_objLstSourc.DataSource =_objTempSrc;
			_objLstSourc.DataTextField ="value";
			_objLstSourc.DataValueField ="key";
			_objLstSourc.DataBind() ;
			//thực hiện add sự kiện javascript cho ảnh
			string _sFunction="changeImage('Images/"+ strAuthor +"/',this.options[this.selectedIndex].value);";
			_objLstSourc.Attributes.Add("Onclick",_sFunction) ;
			
			_objLstSourc.SelectedIndex =0;
			
			sPicture="Images/"+ strAuthor+ "/" +_objLstSourc.SelectedValue ;
						
		}
		protected void cmdUpLoad_Click(object sender, System.EventArgs e)
		{
		   //string _sFolder=Server.MapPath("Images/Share");
		   string strFileUploaded= UploadFile(fPicture.PostedFile,sFolder);
		   SourcList(lstPicture,cboMediaType.SelectedValue,sFolder);
			
		}
		private DataTable MediaType()
		{
			DataTable objTblResult=new DataTable("ListType");
			objTblResult.Columns.Add("TypeID");
			objTblResult.Columns.Add("TypeName");
			string [] strIDs={"1","2","3"};
			string [] strTypes={"Định dạng ảnh","Định dạng flash","Định dạng phim"};
			for(int i=0; i<strIDs.Length; i++)
			{
				object [] objRows=new object[2];
				objRows[0]=strIDs[i];
				objRows[1]=strTypes[i];
				objTblResult.Rows.Add(objRows);
			}
			return objTblResult;
		}
		private string UploadFile(HttpPostedFile _objPostFile,string sFolder)
		{
		   string _sResult="";
		   if(!System.IO.Directory.Exists(sFolder) )
		   {
		   	  System.IO.Directory.CreateDirectory(sFolder); 
		   }
		   int FileCount=System.IO.Directory.GetFiles(sFolder).Length+1;
		   string _sFileName=System.IO.Path.GetFileName(_objPostFile.FileName );
			try
			{
				System.IO.FileInfo _objFile = new FileInfo(System.IO.Path.GetFileName(_objPostFile.FileName ));
				string strEx= _objFile.Extension;
				_objPostFile.SaveAs(sFolder + "\\" +strAuthor+FileCount+strEx) ;
				_sResult=sFolder + "\\" +strAuthor+FileCount+strEx;
			}
			catch (Exception)
			{
				_sResult="";
			}
			return _sResult;
		}

		protected void Button1_Click(object sender, System.EventArgs e)
		{
			string _sFolder=Server.MapPath("Images/Share");
			string _sFile=_sFolder + "\\" + lstPicture.SelectedValue;
			System.IO.File.Delete(_sFile) ;
			SourcList(lstPicture, cboMediaType.SelectedValue, sFolder);
		}
		protected void cboMediaType_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			SourcList(lstPicture,cboMediaType.SelectedValue, sFolder);
		}
	}
}
